package cn.picclife.mwx.manager.sys.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.picclife.mwx.manager.sys.mapper.FlowPathMapper;
import cn.picclife.mwx.manager.sys.mapper.PanelPointMapper;
import cn.picclife.mwx.manager.sys.model.po.FlowPath;
import cn.picclife.mwx.manager.sys.model.po.PanelPoint;
import cn.picclife.mwx.manager.sys.service.FlowPathService;

/**
 * @ClassName FlowPathServiceImpl.java
 * @Description 流程基本信息业务层实现类
 * @author Guan JunHui
 * @Date 2017年9月13日
 */
@Service
public class FlowPathServiceImpl implements FlowPathService {

	@Autowired
	private FlowPathMapper flowPathMapper;
	
	@Autowired
	private PanelPointMapper panelPointMapper;
	
	/**
	 * 添加流程基本信息
	 */
	@Override
	public int insertFlowPath(FlowPath flowPath) {
		flowPath.setCreateTime(new Date());
		return flowPathMapper.insertFlowPath(flowPath);
	}

	/**
	 * 根据主键进行查询
	 */
	@Override
	public Map<String, Object> searchFlowPathById(int flowPath_Id) {
		return flowPathMapper.searchByFlowPathId(flowPath_Id);
	}

	/**
	 * 修改FlowPath
	 */
	@Override
	public int updateFlowPath(FlowPath flowPath) {
		flowPath.setEditTime(new Date());
		return flowPathMapper.updateFlowPath(flowPath);
	}

	/**
	 * 删除
	 */
	@Override
	public int deleteFlowPath(int[]flowPath_Id) {
		int num = 0;
		//将子表中的节点信息全部删除  再删除主表中的基本信息
		try {
			for (int i : flowPath_Id) {
				List<PanelPoint>list = panelPointMapper.queryPanelPoint(i);
				for (PanelPoint panelPoint : list) {
					panelPointMapper.deletePanelPoint(panelPoint.getPanelpoint_id());
				}
				flowPathMapper.deleteFlowPath(i);
			}
			num = 1;
		} catch (Exception e) {
			num=0;
		}
		return num;
	}

	/**
	 * 分页查询方法
	 */
	@Override
	public Map<String, Object> queryFlowPath(FlowPath flowPath) {
		
		Map<String, Object> map = new HashMap<String,Object>();
		
		flowPath.setTotal(flowPathMapper.countFlowPath(flowPath));
		int pageSize = flowPath.getTotal() / flowPath.getPageCount();
		if (flowPath.getTotal() % flowPath.getPageCount()>0) {
			pageSize++;
		}
		flowPath.setPageSize(pageSize);
		flowPath.setSkipNumber(flowPath.getPageCount()*(flowPath.getPageNumber()-1));
		map.put("total", flowPath.getTotal());
		map.put("pageNumber", flowPath.getPageNumber());
		map.put("pageSize", flowPath.getPageSize());
		map.put("list", flowPathMapper.queryFlowPath(flowPath));
		return map;
	}

	@Override
	public FlowPath queryFlowPathForUpdate(FlowPath flowPath) {
		return flowPathMapper.queryFlowPathForUpdate(flowPath);
	}

}
